/*
 * Copyright © 2011 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.midlets;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.senseforce.classes.BouncingBallCanvas;

/**
 * @author jcccn
 * @site http://www.senseforce.com/
 */
 
public class BouncingBall extends MIDlet implements CommandListener {
	private Display display;
	private BouncingBallCanvas canvas;
	
	private Command exitCommand = new Command("Exit", Command.EXIT, 0);
	private Command pauseCommand = new Command("Pause", Command.SCREEN, 1);
	private Command resumeCommand = new Command("Resume", Command.SCREEN, 1);
	
    public void startApp() {
		display = Display.getDisplay(this);
		canvas = new BouncingBallCanvas();
		display.setCurrent(canvas);
		
		canvas.setCommandListener(this);
		canvas.addCommand(exitCommand);
		canvas.addCommand(pauseCommand);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
	
	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {
			try {
				destroyApp(true);
			} catch(Exception ex) {
				
			}
			notifyDestroyed();
		}
		else if (c == pauseCommand) {
			if (canvas != null) {
				canvas.pauseBouncing();
				canvas.removeCommand(pauseCommand);
				canvas.addCommand(resumeCommand);
			}
		}
		else if (c == resumeCommand) {
			if (canvas != null) {
				canvas.resumeBouncing();
				canvas.removeCommand(resumeCommand);
				canvas.addCommand(pauseCommand);
			}
		}
	}
}
